$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var tianjian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["tianjian"];
    var fujian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["fujian"];
    var guangxi = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangxi"];
    var xinsanli = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["xinsanli"];
    var guangzhoumatou = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangzhoumatou"];
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    var warehouseCode = [];
    if (tianjian) {
        warehouseCode.push("W0402011")
    }
    if (fujian) {
        warehouseCode.push("W0402005")
    }
    if (guangxi) {
        warehouseCode.push("W0402006")
    }
    if (xinsanli) {
        warehouseCode.push("W0402004")
    }
    if (guangzhoumatou) {
        warehouseCode.push("W0402010")
    }

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "transfer.html";
    });

    //扫描调拨单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var code = $(this).val();
            if (!code) return $alert("调拨单号不可为空");
            getTransferorders(code);
        }
    });

    //获取调拨单列表
    getTransferorders("");

    function getTransferorders(code) {
        //清空页面中的发货单
        $("#work_list").find("li").remove();
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            url: url + "/app/transfer/getTransferorders",
            type: "post",
            data: {
                code: code,
                warehouseCode: JSON.stringify(warehouseCode)
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的发货单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    if (data.length == 0) return $alert("暂无调拨单");
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">调拨单号：<span class="sp work_code">' + data[i].orderNumber + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<p class="p_none"><span>productCode:</span><span class="sp goods_productCode">' + data[i].inventoryCode + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>调拨前车间:</span><span class="sp goods_house">' + data[i].transferShopBefore + '</span></p>' +
                            '<p><span>调拨前仓库:</span><span class="sp goods_house">' + data[i].transferWarehouseBefore + '</span></p>' +
                            '<p><span>调拨后车间:</span><span class="sp goods_house">' + data[i].transferShopAfter + '</span></p>' +
                            '<p><span>调拨后仓库:</span><span class="sp goods_house">' + data[i].transferWarehouseAfter + '</span></p>';
                        if (data[i].containerNumber) {
                            data_html += '<p><span>集装箱号:</span><span class="sp containerNumber">' + data[i].containerNumber + '</span></p>';
                        }
                        data_html += '<p><span>编码:</span><span class="sp goods_productCode">' + data[i].inventoryCode + '</span></p>' +
                            '<p><span>名称:</span><span class="sp goods_productName">' + data[i].inventoryName + '</span></p>' +
                            '<p><span>规格:</span><span class="sp goods_productSpecification">' + data[i].inventorySpecification + '</span>' +
                            '<span>钢号:</span><span class="sp goods_inventorySteel">' + (data[i].inventorySteel || "") + '</span></p>' +
                            '<p><span>剩余接收:</span><span class="sp goods_planCount">' + (accSub(data[i].actualTransferCount || 0, data[i].actualTransferCount || 0)) + '</span>件' +
                            '<span></span><span class="sp goods_planWeight">' + (accSub(data[i].transferWeights || 0, data[i].actualReceiveCount || 0)) + '</span>T</p>' +
                            '<p><span>制单日期:</span><span class="sp goods_createDate">' + data[i].createTime.split(" ")[0] + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    $alert("请求超时，请再试一次！");
                } else {
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    var work = "";
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var code = $(this).parent().find("span.work_code").html();
        var productName = $(this).parent().find("span.goods_productName").html();
        var productSpecification = $(this).parent().find("span.goods_productSpecification").html();
        var planCount = $(this).parent().find("span.goods_planCount").html();
        var planWeight = $(this).parent().find("span.goods_planWeight").html();
        work = {
            // locationInventorys: data,
            id: id,
            code: code,
            productName: productName,
            productSpecification: productSpecification,
            planCount: planCount,
            planWeight: planWeight
        };
        $(".mengceng").show();
        $("div.div_huanban").show();
        $("div.div_huanban input.QR").val("").focus();
    });
    //点击 取消
    $("div.div_huanban .hb_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_huanban").hide();
    });
    //点击 确认
    $("div.div_huanban input.QR").on("keyup", function (event) {
        if (event.keyCode == "13") {
            selectTransfer();
        }
    });
    $("div.div_huanban .hb_queding").click(function () {
        selectTransfer();
    });
    function selectTransfer() {
        if (!$("div.div_huanban input.QR").val()) {
            $alert("请扫描司机二维码！");
            $("div.div_huanban input.QR").val("").focus();
        } else {
            loading_show("选择中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + "/app/transfer/selectTransfer",
                data: {
                    _id: work.id,
                    QR: $("div.div_huanban input.QR").val()
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        loading_hide();
                        work.locationInventorys = res.result;
                        work.driver_qrcode_bill_no = res.driver_qrcode.bill_no;
                        work.driver_qrcode_count = res.driver_qrcode.count;
                        localStorage.setItem("work", JSON.stringify(work));
                        window.location.href = "transfer_one.html";
                    } else {
                        loading_hide();
                        $alert(res.message);
                        $("div.div_huanban input.QR").val("").focus();
                    }
                },
                error: function (error) {
                    loading_hide();
                    if (error.status == 0) {
                        //loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        //loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        }
    }

});